package willevaluate.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;


import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;


import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.BitmapUtils;
import utils.ShowShareDialog;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/5/8.
 * 填志愿首页
 */

@ContentView(R.layout.evaluate_index)
public class EvaluateIndex extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.complete_will_info_count_edt)
    private EditText complete_will_info_count_edt;
    @ViewInject(R.id.complete_will_info_rank_edt)
    private EditText complete_will_info_rank_edt;
    @ViewInject(R.id.complete_will_info_radioGroup)
    private RadioGroup complete_will_info_radioGroup;
    @ViewInject(R.id.complete_will_info_confirm)
    private Button complete_will_info_confirm;
    @ViewInject(R.id.evaluate_index_button)
    private RelativeLayout evaluate_index_button;
    @ViewInject(R.id.evaluate_index_share_layout)
    private RelativeLayout evaluate_index_share_layout;
    @ViewInject(R.id.evaluate_index_imas_layout)
    private LinearLayout evaluate_index_imas_layout;

    private String type;//文理科
    private String count;//分数
    private String rank;//排名

    private static final int SHARE = 3;
    private static final int SHARE_SUCCESS = 4;

    private MyHandler myHandler;
    private boolean disappearCover;//是否将分享页隐藏
    private ShowShareDialog showShareDialog;

    private static class MyHandler extends Handler {
        WeakReference<EvaluateIndex> weakReference;

        public MyHandler(EvaluateIndex weakReference) {
            this.weakReference = new WeakReference<>(weakReference);
        }

        @Override
        public void handleMessage(Message msg) {
            final EvaluateIndex activity = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回

                    switch (msg.arg1) {
                        case SHARE:
                            try {
                                JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                                JSONObject param = jsonObject.getJSONObject("param");
                                final String title = param.getString("title");
                                String image = param.getString("image");
                                String content = param.getString("content");
                                String url = param.getString("url");
                                activity.showShareDialog.setUrl(url);
                                activity.showShareDialog.setTitle(title);
                                activity.showShareDialog.setContent(content);
                                activity.showShareDialog.setImage(image);
                                activity.showShareDialog.show();
                                activity.showShareDialog.setOnShareClickListener(new ShowShareDialog.OnShareClickListener() {
                                    @Override
                                    public void onShareClicklistener() {
                                        activity.shareSuccess();
                                    }
                                });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        case SHARE_SUCCESS:
                            try {
                                JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                                String err_code = jsonObject.getString("err_code");
                                if ("0".equals(err_code)) {
                                    activity.disappearCover = true;
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }


        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myHandler = new MyHandler(this);
        HashMap<String, String> hashMap = (HashMap<String, String>) getIntent().getSerializableExtra("params");
        String isShare = hashMap.get("isShare");
        if ("1".equals(isShare)) {
            //已经分享
            initView(false);
            disappearCover = true;
        } else {
            //未分享
            initView(true);
            disappearCover = false;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (showShareDialog != null) {
            showShareDialog.dismiss();
        }
        if (disappearCover) {
            evaluate_index_share_layout.setVisibility(View.GONE);
        } else {
            evaluate_index_share_layout.setVisibility(View.VISIBLE);
        }
    }

    private void initView(boolean showShare) {
        showShareDialog = new ShowShareDialog(this, R.style.centerDialog);
        if (showShare) {
            //显示介绍页
            evaluate_index_share_layout.setVisibility(View.VISIBLE);
            int[] imas = new int[]{R.mipmap.evaluate_index_cover_ima1,
                    R.mipmap.evaluate_index_cover_ima2,
                    R.mipmap.evaluate_index_cover_ima3,
                    R.mipmap.evaluate_index_cover_ima4
            };

            if (evaluate_index_imas_layout.getChildCount() > 0) {
                for (int i = 0; i < evaluate_index_imas_layout.getChildCount(); i++) {
                    InputStream is = getResources().openRawResource(imas[i]);
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    ((ImageView) evaluate_index_imas_layout.getChildAt(i)).setImageBitmap(bitmap);
                    ((ImageView) evaluate_index_imas_layout.getChildAt(i)).setAdjustViewBounds(true);
                    ((ImageView) evaluate_index_imas_layout.getChildAt(i)).setScaleType(ImageView.ScaleType.FIT_XY);
                }
            }
        } else {
            evaluate_index_share_layout.setVisibility(View.GONE);
        }
        common_title.setText("志愿评估");
        common_back.setOnClickListener(this);
        complete_will_info_confirm.setOnClickListener(this);
        evaluate_index_button.setOnClickListener(this);
        complete_will_info_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.complete_will_info_btn1:
                        type = "文科";
                        break;
                    case R.id.complete_will_info_btn2:
                        type = "理科";
                        break;
                    default:
                        break;
                }
            }
        });
        type = "文科";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recycleBitmap();
    }

    private void recycleBitmap() {
        for (int i = 0; i < evaluate_index_imas_layout.getChildCount(); i++) {
            Drawable drawable = ((ImageView) evaluate_index_imas_layout.getChildAt(i)).getDrawable();
            BitmapUtils.recycleBitmap(drawable);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.evaluate_index_button:
                if (EvaluateIndex.this.hasLoad()) {
                    //去分享
                    share();
                } else {
                    EvaluateIndex.this.login(EvaluateIndex.this);
                }
                break;

            case R.id.complete_will_info_confirm:
                if (EvaluateIndex.this.hasLoad()) {
                    count = complete_will_info_count_edt.getText().toString();
                    rank = complete_will_info_rank_edt.getText().toString();
                    if (TextUtils.isEmpty(count)) {
                        ToastUtils.shortToast("分数不能为空");
                    } else if (TextUtils.isEmpty(rank)) {
                        ToastUtils.shortToast("排名不能为空");
                    } else {
                        Intent i = new Intent(EvaluateIndex.this, WillList.class);
                        i.putExtra("type", type);
                        i.putExtra("count", count);
                        i.putExtra("rank", rank);
                        startActivity(i);
                    }
                } else {
                    EvaluateIndex.this.login(EvaluateIndex.this);
                }
                break;
            case R.id.common_back:
                this.finish();
                break;
            default:
                break;
        }
    }

    //分享
    private void share() {
        new InterNetController(EvaluateIndex.this, Constant.SHAREEVALUATE, myHandler, null, SHARE, false);
    }

    //分享成功
    private void shareSuccess() {

        new InterNetController(EvaluateIndex.this, Constant.SHAREEVALUATESUCCESS, myHandler, null, SHARE_SUCCESS, false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.LOGIN_REQUEST_CODE) {
            //登录成功
            EvaluateIndex.this.finish();
        } else {
            if (resultCode != RESULT_CANCELED) {
                if (resultCode == Constant.PAY_SUCCESS) {
                    initView(false);
                }
            }
        }
    }
}
